﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CW.Bll.TrafficVideoGather.GatherClient;
using CW.Common.TrafficVideoGather.Logging;

namespace CW.TrafficVideoGather.App.GatherClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private List<IntPtr> _chanHandleList = new List<IntPtr>();
        private int g_nTotalDspCnt;
        private int g_nTotalEncChannels;
        private int g_nTotalDecChannels;
        private int g_nTotalDispChannels;
        private int g_iMotionRectNum = 100;//移动监测数量
        private int g_nEncCurrentPort = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //设置视频模式
                int result = DS40SdkApi.SetDefaultVideoStandard(DS40SdkApi.VideoStandard_t.StandardPAL);
                if (result != 0)
                {
                    SetLogTxt("设置默认的视频制式失败!" + result);
                    return;
                }
                result = DS40SdkApi.SetDefaultHDVideoStandard(DS40SdkApi.VideoStandard_t.StandardPAL);
                if (result != 0)
                {
                    SetLogTxt("设置默认的高清视频制式失败!" + result);
                    return;
                }
                result = DS40SdkApi.InitDSPs();
                if (result != 0)
                {
                    SetLogTxt("初始化板卡失败!" + result);
                    return;
                }

                g_nTotalDspCnt = DS40SdkApi.GetDspCount();
                g_nTotalEncChannels = DS40SdkApi.GetEncodeChannelCount();
                g_nTotalDecChannels = DS40SdkApi.GetDecodeChannelCount();
                g_nTotalDispChannels = DS40SdkApi.GetDisplayChannelCount();

                //打开通道

                for (int i = 0; i < g_nTotalEncChannels; i++)
                {
                    _chanHandleList.Add(DS40SdkApi.ChannelOpen(i));

                    //注册画图回调函数
                    DS40SdkApi.RegisterDrawFun(i, DRAWFUN, i);

                    //
                    DS40SdkApi.RegisterStreamDirectReadCallback(StreamDirectReadCallback, IntPtr.Zero);
                }

            }
            catch (Exception ex)
            {
                _rtxtLog.AppendText(ex.Message + Environment.NewLine);
            }
        }

        private int StreamDirectReadCallback(int channelNumber, byte[] dataBuf, int length, int frameType, IntPtr context)
        {
            if (frameType == (int)DS40SdkApi.FrameType_t.PktMotionDetection)
            {
                int[] result = new int[100];
                int g_nThreshold = 2;//运动阀值
                DS40SdkApi.MotionAnalyzer(_chanHandleList[channelNumber], dataBuf, g_nThreshold, result);
                /*对每个区域的移动侦测进行分析，返回result，如果result为1，则输出打印信息*/
                string resultStr = "";
                for (int i = 0; i < g_iMotionRectNum; i++)
                {
                    if (result[i] == 1)
                    {
                        if (channelNumber == g_nEncCurrentPort)
                        {
                            resultStr += result[i] + "|";
                        }
                    }
                }
                SetLogTxt("移动区域大小：" + resultStr);
            }
        }

        public void DRAWFUN(long nPort, IntPtr hDc, long nUser)
        {
            SetLogTxt("触发画图回调函数");
        }

        private void SetLogTxt(string log)
        {
            _rtxtLog.AppendText(log);
            Logger.GetLogger("").Error(log);
        }

    }
}
